import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  test: {
    environment: 'jsdom',
    setupFiles: ['./src/test-setup.js'],
    globals: true,
    css: true,
    
    // 🚀 性能优化配置
    pool: 'threads', // 使用线程池并行执行
    poolOptions: {
      threads: {
        singleThread: false, // 启用多线程
        maxThreads: 4, // 最大线程数
        minThreads: 1, // 最小线程数
        isolate: true // 隔离测试环境
      }
    },
    
    // 📁 测试文件匹配
    include: ['src/**/*.{test,spec}.{js,jsx,ts,tsx}'],
    exclude: [
      'node_modules/**',
      'dist/**',
      'build/**',
      '**/*.config.{js,ts}',
      'src/test-setup.js',
      'coverage/**'
    ],
    
    // 📊 覆盖率配置
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html', 'lcov', 'text-summary'],
      reportsDirectory: './coverage',
      exclude: [
        'node_modules/',
        'src/test-setup.js',
        'src/test-utils.jsx',
        '**/*.test.{js,jsx,ts,tsx}',
        '**/*.config.{js,ts}',
        'dist/',
        'build/',
        'coverage/',
        '**/*.d.ts',
        '**/*.stories.{js,jsx,ts,tsx}'
      ],
      // 🎯 覆盖率阈值 - 企业级标准
      thresholds: {
        global: {
          branches: 50,
          functions: 50,
          lines: 50,
          statements: 50
        },
        // 关键文件更高要求
        'src/App.jsx': {
          branches: 70,
          functions: 70,
          lines: 70,
          statements: 70
        },
        'src/components/Navigation.jsx': {
          branches: 80,
          functions: 80,
          lines: 80,
          statements: 80
        },
        'src/pages/HomePage.jsx': {
          branches: 70,
          functions: 70,
          lines: 70,
          statements: 70
        }
      }
    },
    
    // ⏱️ 超时和重试配置
    testTimeout: 15000, // 增加超时时间
    hookTimeout: 10000,
    teardownTimeout: 5000,
    
    // 🔄 重试机制
    retry: 2, // 失败时重试2次
    bail: 0, // 不因单个测试失败而停止
    
    // 🎭 Mock 配置
    mockReset: true,
    clearMocks: true,
    restoreMocks: true,
    
    // 💾 缓存配置
    cache: {
      dir: './node_modules/.vitest',
      include: ['**/*.test.{js,jsx,ts,tsx}', '**/*.spec.{js,jsx,ts,tsx}']
    },
    
    // 👀 监听模式配置
    watch: false,
    
    // 📋 报告配置
    reporter: ['verbose', 'json', 'html', 'junit'],
    outputFile: {
      json: './test-results.json',
      html: './test-report.html',
      junit: './test-results.xml'
    },
    
    // 🔍 测试发现配置
    passWithNoTests: true,
    
    // 📈 性能监控
    benchmark: {
      outputFile: './benchmark-results.json'
    }
  },
  resolve: {
    alias: {
      '@': '/src'
    }
  }
})